package com.jieli.bt.att.ui.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.jieli.bt.att.R
import com.jieli.bt.att.databinding.ActivityHomeBinding
import com.jieli.bt.att.tool.ble.BleManager
import com.jieli.bt.att.tool.scan.BtScanner
import com.jieli.bt.att.ui.common.basic.BasicActivity
import com.jieli.bt.att.ui.common.basic.BasicFragment
import com.jieli.bt.att.ui.device.DeviceFragment
import com.jieli.bt.att.ui.settings.SettingsFragment
import com.jieli.bt.att.util.ViewUtil
import com.jieli.logcat.JL_Log
import kotlin.system.exitProcess

/**
 * @author zqjasonZhong
 * @since 2025/3/27
 * @email zhongzhuocheng@zh-jieli.com
 * @desc 主页
 */
class HomeActivity : BasicActivity() {
    private lateinit var binding: ActivityHomeBinding

    private val fragments = mutableListOf<BasicFragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        ViewUtil.setImmersiveStateBar(window, true)
        super.onCreate(savedInstanceState)
        binding = ActivityHomeBinding.inflate(layoutInflater)
        setContentView(binding.root)
        JL_Log.i(
            TAG,
            "onCreate",
            "App Version Name : ${ViewUtil.getAppVersionName(this)}, App Version Code : ${
                ViewUtil.getAppVersion(this)
            }"
        )
        initUI()
    }

    @Deprecated("Deprecated in Java")
    override fun onBackPressed() {
        super.onBackPressed()
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        BtScanner.getInstance().destroy()
        BleManager.getInstance().destroy()
        exitProcess(0)
    }

    private fun initUI() {
        fragments.add(DeviceFragment.newInstance())
        fragments.add(SettingsFragment.newInstance())

        binding.vp2Container.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageScrollStateChanged(state: Int) {
                if (state == ViewPager2.SCROLL_STATE_IDLE) {
                    binding.bnvBottom.selectedItemId =
                        binding.bnvBottom.menu.getItem(binding.vp2Container.currentItem).itemId
                }
            }
        })
        binding.vp2Container.isUserInputEnabled = false
        binding.vp2Container.adapter =
            ViewPagerAdapter(supportFragmentManager, lifecycle, fragments)

        binding.bnvBottom.itemIconTintList = null
        binding.bnvBottom.setOnItemSelectedListener {
            val index = when (it.itemId) {
                R.id.tab_settings -> 1
                else -> 0
            }
            binding.vp2Container.setCurrentItem(index, false)
            return@setOnItemSelectedListener true
        }
    }

    private class ViewPagerAdapter(
        fragmentManager: FragmentManager, lifecycle: Lifecycle,
        private val fragments: MutableList<BasicFragment>
    ) :
        FragmentStateAdapter(fragmentManager, lifecycle) {
        override fun getItemCount(): Int = fragments.size

        override fun createFragment(position: Int): Fragment = fragments[position]
    }
}